package com.zhiche.lisa.lspm.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.driver.Driver;
import com.zhiche.lisa.lspm.dao.model.driver.bo.DriverCheckinLineBO;
import com.zhiche.lisa.lspm.dao.model.fleet.FleetAdmin;
import com.zhiche.lisa.lspm.dto.DriverCheckinDTO;
import com.zhiche.lisa.lspm.service.fleet.FleetAdminService;
import com.zhiche.lisa.lspm.vo.driver.DriverCheckinLineVO;
import com.zhiche.lisa.lspm.vo.fleet.FleetAdminVO;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 车队管理员表 前端控制器
 * </p>
 *
 * @author hongbo
 * @since 2018-10-29
 */
@RestController
@RequestMapping("/fleetAdmin")
@Api(value = "/fleetAdmin", description = "车队管理", tags = {"车队管理"})
public class FleetAdminController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FleetAdminController.class);

    @Autowired
    FleetAdminService fleetAdminService;

    @PostMapping(value = "/addFleetAdmin")
    @ApiOperation(value = "添加车队管理员", notes = "添加车队管理员", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> createFleet(@RequestBody @ApiParam(name = "添加车队管理员对象", value = "传入json格式", required = true) FleetAdmin fleetAdmin, @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetAdminController.createFleetAdmin（添加车队管理员） params : {} ", fleetAdmin);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum
                .SUCCESS.getText(), null);
        try {
            fleetAdminService.addFleetAdmin(fleetAdmin, authorization);
        } catch (BaseException e) {
            LOGGER.error("FleetController.createFleet ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetController.createFleet ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/queryFleetAdminList")
    @ApiOperation(value = "获取车队管理员列表", notes = "获取车队管理员列表",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<FleetAdminVO>> queryFleetAdminList(@RequestBody Page<FleetAdminVO> page,
                                                                    @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetAdminController.queryFleetAdminList（获取车队管理员列表） params : {} ", page);

        RestfulResponse<Page<FleetAdminVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            result.setData(fleetAdminService.queryFleetAdminList(page,authorization));
        } catch (BaseException e) {
            LOGGER.error("FleetAdminController.queryFleetAdminList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetAdminController.queryFleetAdminList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    @GetMapping(value = "/queryAccountByPhone")
    @ApiOperation(value = "根据手机号查询账号信息", notes = "根据手机号查询账号信息",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Driver> queryAccountByPhone(@RequestParam String phone,
                                                       @RequestHeader("Authorization") String authorization) {
        LOGGER.info("FleetAdminController.queryAccountByPhone（根据手机号查询账号信息） params : {} ", phone);

        RestfulResponse<Driver> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);

        try {
            result.setData(fleetAdminService.queryAccountByPhone(phone));
        } catch (BaseException e) {
            LOGGER.error("FleetAdminController.queryAccountByPhone ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetAdminController.queryAccountByPhone ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }

        return result;
    }

    /**
     * 车队车辆报班 - 新增报班
     */
    @PostMapping("/fleetCheckIn")
    @ApiOperation(value = "车队车辆报班", notes = "车队车辆报班",
            produces = MediaType.APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    @ApiResponses({@ApiResponse(code = 0, message = "success"), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Object> fleetCheckIn(@RequestBody DriverCheckinDTO dto,
                                                                    @RequestHeader("Authorization") String token) {
        RestfulResponse<Object> response = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(),
                CommonEnum.SUCCESS.getText(), null);
        LOGGER.info("FleetAdminController.fleetCheckIn param: {}", JSONObject.toJSONString(dto));

        try {
            fleetAdminService.fleetCheckIn(dto, token);
        } catch (BaseException be) {
            LOGGER.error("FleetAdminController.fleetCheckIn BaseException... ", be);
            response.setCode(CommonEnum.ERROR.getCode());
            response.setMessage(be.getMessage());
        } catch (Exception e) {
            LOGGER.error("FleetAdminController.fleetCheckIn error... ", e);
            response.setCode(CommonEnum.ERROR.getCode());
            response.setMessage(CommonEnum.ERROR.getText());
        }
        return response;
    }


    @PostMapping(value = "/fleetCheckinLine")
    @ApiOperation(value = "获取车队车辆报班记录列表", notes = "获取车队车辆报班记录列表", produces = MediaType
            .APPLICATION_JSON_VALUE, response = RestfulResponse.class)
    public RestfulResponse<Page<DriverCheckinLineVO>> fleetCheckinLine(@RequestBody Page<DriverCheckinLineBO> page) {
        LOGGER.info("FleetAdminController.fleetCheckinLine（获取车队车辆报班记录列表） params : {} ", page);
        RestfulResponse<Page<DriverCheckinLineVO>> result = new RestfulResponse<>(CommonEnum
                .SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            result.setData(fleetAdminService.fleetCheckinLine(page));
        } catch (BaseException e) {
            LOGGER.error("FleetAdminController.fleetCheckinLine ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("FleetAdminController.fleetCheckinLine ERROR... ", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }
}

